<template>
  <div>
    <el-drawer v-model="drawer" :before-close="handleClose" :close-on-press-escape="false" :direction="direction"
               :title="title" :wrapperClosable="false" size="1250px">
      <div style="margin-left: 20px">
        号别名称：<span class="span-font">{{ row.name }}</span>
        / 门诊科室：<span class="span-font">{{ row.deptName }}</span>
        / 所属医生：<span class="span-font">{{ row.doctorName }}</span>
        / 诊疗项目：<span class="span-font">{{ row.clinicItemName }}</span>
      </div>
      <div style="margin-top: 20px;margin-left: 22px;">
        <template v-for="(dict) in clinicVisitSchedule">
          <el-row>
            <el-card class="el-card-height" shadow="never">
              <div>{{ dict.dictLabel }}</div>
            </el-card>
            <div style="display: flex;margin-left:150px;margin-top: -150px;">
              <el-col v-for="(info, index) in rowInfo" :key="index" :span="3.5">
                <el-card class="el-card-height" shadow="never" style="flex: 1;">
                  <div class="div-bottom">
                    <span :style="info.cssClass">{{ info.dictLabel }}</span>
                    <el-checkbox v-model="checkList" :label="dict.dictValue + '-' + info.dictValue"
                                 style="margin-left: 5px"
                                 @change="checkChange($event, dict.dictValue + '-' + info.dictValue)">
                      <br>
                    </el-checkbox>
                  </div>
                  <div>
                    <el-input v-model="inputData[dict.dictValue + '-' + info.dictValue + '-limitNum']"
                              :disabled="checkChangeKey.indexOf(dict.dictValue + '-' + info.dictValue) > -1"
                              class="input-margin-bottom" clearable placeholder="限号数"
                              @blur="limitNum($event, dict.dictValue + '-' + info.dictValue, 'limitNum')"/>
                    <el-input
                      v-model="inputData[dict.dictValue + '-' + info.dictValue + '-limitPreNum']"
                      :disabled="checkChangeKey.indexOf(dict.dictValue + '-' + info.dictValue) > -1"
                      class="input-margin-bottom" clearable placeholder="预约限号数"
                      @blur="limitNum($event, dict.dictValue + '-' + info.dictValue, 'limitPreNum')"/>
                    <el-input v-show="false"
                              v-model="inputData[dict.dictValue + '-' + info.dictValue + '-schedule']"/>
                  </div>
                </el-card>
              </el-col>
            </div>
          </el-row>
        </template>
        <div slot="footer" class="dialog-footer-this">
          <el-button :loading="buttonLoading" type="primary" @click="submitForm">保存出诊安排</el-button>
          <el-button @click="handleClose">取 消</el-button>
        </div>
      </div>
    </el-drawer>
  </div>
</template>

<script lang="ts" setup>
import {getDicts} from '@/api/system/dict/data'
import {addClinicSchedule, selectPageClinicSchedule} from '@/api/base/clinicSchedule'
import {useDictStoreHook} from "@/store/modules/dict";

const dictStore = useDictStoreHook();
const dictDataMap = ref(dictStore.isTypeGetData('clinic_visit_schedule'))
const drawer = ref(false)
const direction = ref('rtl')
const title = ref('出诊安排(限号数、预约限号数为空或者填写0，表示不限号)')
// 按钮loading
const buttonLoading = ref(false)
const row = ref<any>({})
// 号表出诊时段数组
const clinicVisitSchedule = ref<any>([])
// 选中的单选按钮
const checkList = ref<any>([])
// 遍历的input对应的 v-modal 值
const inputData = ref({})
// 保存的list数据
const saveInfoList = ref<any>([])
// 多选按钮点击事件值
const checkChangeKey = ref<any>([])
const rowInfo = ref<any>([])

init()

function handleClose() {
  checkList.value = []
  checkChangeKey.value = []
  saveInfoList.value = []
  inputData.value = []
  init()
  drawer.value = false
}

/** 初始化input数据 */
function init() {
  getDicts('week_dict').then((resA: any) => {
    rowInfo.value = resA.data
    getDicts('clinic_visit_schedule').then((res: any) => {
      clinicVisitSchedule.value = res.data
      let aa = {}
      clinicVisitSchedule.value.forEach((dict: any) => {
        rowInfo.value.forEach(info => {
          let key = dict.dictValue + '-' + info.dictValue
          aa[key + '-limitNum'] = ''
          aa[key + '-limitPreNum'] = ''
          // 设置 input 禁用list
          checkChangeKey.value.push(key)
        })
      })
      inputData.value = aa
    })
  })
}

/** 打开弹出框 */
function showClinicScheduleDrawerFun(rowInfo) {
  const rowRaw = toRaw(rowInfo)
  row.value = rowRaw
  let obj: any = {
    pageNum: 1,
    pageSize: 50,
    clinicIndexId: rowRaw.clinicIndexId,
    orgId: rowRaw.orgId
  }
  selectPageClinicSchedule(obj).then((res: any) => {
    const rows = res.rows
    rows.forEach(item => {
      let key = item.timeDesc + '-' + item.dayOfWeek
      let limitNumKey = key + '-limitNum'
      let limitPreNumKey = key + '-limitPreNum'
      let scheduleKey = key + '-schedule'
      inputData.value[limitNumKey] = item.limitNum
      inputData.value[limitPreNumKey] = item.limitPreNum
      inputData.value[scheduleKey] = item.scheduleId
      // 设置默认选中
      checkList.value.push(key)
      // 获取数据库存在的key
      let index = checkChangeKey.value.indexOf(key)
      // 已存在的key设置input取消禁用
      checkChangeKey.value.splice(index, 1)
      checkChange(true, key)
    })
    drawer.value = true
  })

}

/** 复选框点击事件 */
function checkChange(event, key) {
  if (event) {
    let info: any = {}
    // 如果取消，设置input不能编辑
    let index = checkChangeKey.value.indexOf(key)
    checkChangeKey.value.splice(index, 1)
    let itemArray = key.split("-")
    info.scheduleId = inputData.value[key + '-schedule']
    info.timeDesc = itemArray[0]
    info.dayOfWeek = itemArray[1]
    info.limitNum = inputData.value[key + '-limitNum']
    info.limitPreNum = inputData.value[key + '-limitPreNum']
    saveInfoList.value.push(info)
  } else {
    // inputData[key + '-limitNum'] = 0
    // inputData[key + '-limitPreNum'] = 0
    // 添加input禁用
    checkChangeKey.value.push(key)
    saveInfoList.forEach(item => {
      let saveKey = item.timeDesc + '-' + item.dayOfWeek
      if (saveKey === key) {
        let index = saveInfoList.value.indexOf(item)
        saveInfoList.value.splice(index, 1)
      }
    })
  }
}

/** 限号input修改事件 */
function limitNum(event, key, key2) {
  saveInfoList.value.forEach(item => {
    let saveKey = item.timeDesc + '-' + item.dayOfWeek
    item.scheduleId = inputData.value[saveKey + '-schedule']
    if (saveKey === key) {
      if (key2 === 'limitNum') {
        item.limitNum = event.target.value
      } else {
        item.limitPreNum = event.target.value
      }
    }
  })
}

/** 提交 */
function submitForm() {
  if (checkList.value.length <= 0) {
    return ElMessage.error("请勾选需要安排的星期后，再保存")
  }
  let data = {
    saveInfoList: saveInfoList.value,
    clinicIndexId: row.value.clinicIndexId,
    orgId: row.value.orgId
  }
  addClinicSchedule(data).then(_res => {
    ElMessage.success("出诊安排保存成功")
    handleClose()
  })
}

defineExpose({
  showClinicScheduleDrawerFun
})
</script>


<script lang="ts">
export default {
  name: "clinicSchedule"
}
</script>

<style scoped>
:deep(.el-drawer) {
  margin-top: 100px;
  margin-right: 20px;
  height: 800px;
}

:deep(.el-drawer__header) {
  background-color: rgb(245, 245, 245);
  padding: 0px 20px;
  height: 50px;
  margin-bottom: 0px;
}

:deep(.el-card__body) {
  padding: 10px 20px 20px 20px;
}

:deep(.el-checkbox__inner) {
  border: 1px solid #000000;
}

.input-margin-bottom {
  margin-bottom: 5px;
}

.div-bottom {
  margin-bottom: 10px;
}

.el-card-height {
  height: 150px;
  width: 150px
}

.dialog-footer-this {
  float: right;
  margin: 10px 30px 0 0;
  padding-bottom: 10px;
}
</style>
